<template>
  <div class="coupon_list-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="margin_right_20">
          优惠券名称:&nbsp;
          <el-input
            v-model="opt1"
            style="width:100px;height:30px;"
            @keyup.enter.native="get_list(1)"
          ></el-input>
        </div>
        <div class="margin_right_20">
          优惠券类型:&nbsp;
          <el-select v-model="opt2" style="width: 100px;">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>

        <div class="margin_right_20">
          优惠券状态:&nbsp;
          <el-select v-model="opt3" style="width: 100px;">
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <div class="margin_right_20">
          有无时间限制:&nbsp;
          <el-select v-model="opt4" style="width: 100px;">
            <el-option
              v-for="item in options4"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>

        <div class="btn">
          <el-button type="primary" icon="el-icon-search" @click="get_list(currentPage)">查询</el-button>
        </div>
      </div>
      <div class="table">
        <el-table :data="couponList" style="width: 100%" height="598">
          <el-table-column width="55">
            <template slot-scope="scope">
              <el-radio-group ref="radioButton" v-model="radio_">
                <el-radio
                  style="width:20px"
                  :label="scope.$index"
                  @change="handleSelectionChange(scope.row.id,scope.$index,scope.row)"
                >&nbsp;</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="优惠券名称"></el-table-column>
          <el-table-column prop="price" label="优惠券面额(元)"></el-table-column>
          <el-table-column prop="couponType" label="类型"></el-table-column>
          <el-table-column prop="take" label="已领取数量"></el-table-column>
          <el-table-column prop="used" label="已使用数量"></el-table-column>
          <el-table-column prop="startTime" label="开始时间"></el-table-column>
          <el-table-column prop="endTime" label="结束时间"></el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.status == 0">{{'启用'}}</div>
              <div v-else>{{'禁用'}}</div>
            </template>
          </el-table-column>
        </el-table>
        <div class="btn">
          <el-button
            style="width:70px;margin-left:600px"
            type="primary"
            @click="submit_coupon_type"
          >确 定</el-button>
          <el-button style="color: #409eff;width:70px;margin-left:100px" @click="$router.go(-1)">取 消</el-button>
        </div>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";

import { coupon_list, coupon_delete } from "../../api/getData.js";
export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      form: {},
      //控制添加对话框的显示和隐藏
      dialogVisible: false,
      page: 1,
      currentPage: 1,
      total: 0,
      page: 1,
      pageSize: 10,
      opt1: "",
      opt2: "",
      opt3: "",
      opt4: "2",
      radio_: "",
      options3: [
        { id: "", label: "全部" },
        { id: "1", label: "可使用" },
        { id: "2", label: "禁用" },
        { id: "3", label: "已过期" }
      ],
      options2: [
        { id: "", label: "全部" },
        { id: "1", label: "全场通用" },
        { id: "2", label: "部分活动通用" },
        { id: "3", label: "部分商品类型通用" },
        { id: "4", label: "部分活动商品类型通用" },
        { id: "5", label: "部分商品可用" }
      ],

      options4: [
        { id: "", label: "全部" },
        { id: "0", label: "无时间限制" },
        { id: "1", label: "有时间限制" },
        { id: "2", label: "有效期" }
      ],
      couponList: [],
      couponMsg: { name: "", id: "" }
    };
  },
  created() {
    this.get_list();
  },
  methods: {
    handleSelectionChange(id, index, scope) {
      this.radio_ = index;
      this.val = scope;
    },
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_list();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_list();
    },

    get_list(currentPage) {
      if (currentPage) {
        this.currentPage = currentPage;
      }
      let data =
        "name=" +
        this.opt1 +
        "&type=" +
        this.opt2 +
        "&typeId=" +
        this.opt3 +
        "&limitKind=" +
        this.opt4 +
        "&current=" +
        this.currentPage;

      coupon_list(data).then(result => {
        this.total = result.data.data.total;
        this.page = Math.ceil(this.total / this.pageSize);
        if (result.data.status == 0) {
          this.couponList = result.data.data.records;
          this.couponList.forEach(item => {
            item["checked"] = false;
            if (item.couponType == "1") {
              item.couponType = "全场通用";
            }
            if (item.couponType == "2") {
              item.couponType = "部分活动通用";
            }
            if (item.couponType == "3") {
              item.couponType = "部分商品类型通用";
            }
            if (item.couponType == "4") {
              item.couponType = "部分活动商品类型通用";
            }
            if (item.couponType == "5") {
              item.couponType = "部分商品可用";
            }
          });
        }
      });
    },
    submit_coupon_type() {
      localStorage.setItem(
        "couponMsg",
        JSON.stringify({ couponId: this.val.id, couponName: this.val.name })
      );

      this.$router.go(-1);
    }
  }
};
</script>


<style lang="less" >
.coupon_list-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  .content {
    .query {
      span.el-radio__label {
        display: none !important;
      }
      .start_time {
        display: flex;
        align-items: center;
      }
      .end_time {
        display: flex;
        align-items: center;
      }
      .btn {
        .el-button {
          width: 70px;
        }
      }
    }
  }
}
</style>

